<?php if( ! defined('BASEPATH')) exit('No direct script access allowed');

class Rating extends CI_Controller{
	function __construct(){
		parent::__construct();
		$this->load->helper(array('form', 'url','html','string'));
        $this->load->library('session');
        //$this->load->library('system_libs');
        $this->config->load('ojexcom');
	}

	function index(){
		$data_output['rating'] = "";
		$providerid = $this->input->post('providerId');
		$count = 0;
		$query = $this->db->query("SELECT reviewerId, u.`name`, rateValue, u.avatar, u.lastUpdatedAvatar, `comment` FROM rating r JOIN `user` u ON u.id=r.reviewerId WHERE providerId = ".$providerid." AND rateValue > 0 AND `comment` IS NOT NULL AND `comment` <> ''");
		$row = $query->row();
		foreach($query->result() as $row){
			$data_output['rating'][$count]['reviewerId'] = $row->reviewerId;
			$data_output['rating'][$count]['name'] = $row->name;
			$data_output['rating'][$count]['avatar'] = $row->avatar;
			$data_output['rating'][$count]['lastUpdatedAvatar'] = $row->lastUpdatedAvatar;
			$data_output['rating'][$count]['rateValue'] = ''.$row->rateValue;
			$data_output['rating'][$count]['comment'] = $row->comment;
			$count++;
		}
		header('Content-Type: application/json');
		echo json_encode($data_output);
	}
	
	function addRating(){
		//cek session
        $isLogin = $this->session->userdata('isLogin');
        $user = ''; $id='';
        if ($isLogin) {
            $user = $this->session->userdata('username');
            $id = $this->session->userdata('user_id');
        }

        //cek segment
        $str_id = '';
        if ($this->uri->segment(3) == FALSE) {
            redirect('home', 'location');
        } else {
            $str_id = $this->uri->segment(3);
        }

		$data_output['success'] = "false";
		//$userid = $this->input->post('userId');
		$userid = $id;
		//$providerid = $this->input->post('providerId');
		$providerid = $str_id;
		$rateValue = $this->input->get('rateValue');
		//$comment = $this->input->post('comment');
		$where_data = array(
			'reviewerId' => $userid,
			'providerid' => $providerid,
		);	
		$set_data = array(
			'rateValue' => $rateValue,
			//'comment' => $comment,
		);	
		$this->db->where($where_data);
		$this->db->update('rating', $set_data);
		if($this->db->affected_rows() < 1){
			$query_notes = $this->db->query("SELECT * FROM `rating` WHERE reviewerId = ".$userid." AND providerid = ".$providerid);
			if ($query_notes->num_rows() < 1){
				$input_data = array(
					'reviewerId' => $userid,
					'providerid' => $providerid,
					'rateValue' => $rateValue,
					//'comment' => $comment,
				);	
				$this->db->insert('rating', $input_data);
				if($this->db->affected_rows() > 0)
					$data_output['success'] = "true";
			} 
		} else {
			$data_output['success'] = "true";
		}

		//echo "Sedang diproses, silakan tunggu sebentar...";
		redirect('profil/detail/'.$str_id,'refresh');
		// header('Content-Type: application/json');
		// echo json_encode($data_output);
	}

	function addComment(){
		//cek session
        $isLogin = $this->session->userdata('isLogin');
        $user = ''; $id='';
        if ($isLogin) {
            $user = $this->session->userdata('username');
            $id = $this->session->userdata('user_id');
        }

        //cek segment
        $str_id = '';
        if ($this->uri->segment(3) == FALSE) {
            redirect('home', 'location');
        } else {
            $str_id = $this->uri->segment(3);
        }

		$data_output['success'] = "false";
		//$userid = $this->input->post('userId');
		$userid = $id;
		//$providerid = $this->input->post('providerId');
		$providerid = $str_id;
		//$rateValue = $this->input->post('rateValue');
		$comment = $this->input->post('comment');
		$where_data = array(
			'reviewerId' => $userid,
			'providerid' => $providerid,
		);	
		$set_data = array(
			//'rateValue' => $rateValue,
			'comment' => $comment,
		);	
		$this->db->where($where_data);
		$this->db->update('comments', $set_data);
		if($this->db->affected_rows() < 1){
			$query_notes = $this->db->query("SELECT * FROM `comments` WHERE reviewerId = ".$userid." AND providerid = ".$providerid);
			if ($query_notes->num_rows() < 1){
				$input_data = array(
					'reviewerId' => $userid,
					'providerid' => $providerid,
					//'rateValue' => $rateValue,
					'comment' => $comment,
				);	
				$this->db->insert('comments', $input_data);
				if($this->db->affected_rows() > 0)
					$data_output['success'] = "true";
			} 
		} else {
			$data_output['success'] = "true";
		}

		//echo "Sedang diproses, silakan tunggu sebentar...";
		redirect('profil/detail/'.$str_id,'refresh');
		// header('Content-Type: application/json');
		// echo json_encode($data_output);
	}
}